package com.airsystem.pos.bengkel.util;

import java.util.Arrays;

import javax.accessibility.Accessible;
import javax.swing.JComboBox;
import javax.swing.JTextField;

import com.airsystem.pos.bengkel.interfaces.IValidator;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class FormValidator implements IValidator<Accessible> {

	public boolean isValid(Accessible... components) throws IllegalArgumentException {
		Preconditions.checkNotNull(components);

		return isValid(Arrays.asList(components));
	}

	public boolean isValid(Iterable<Accessible> components) throws IllegalArgumentException {
		for (Accessible item : components) {
			if (item instanceof JTextField) {
				Preconditions.checkEmpty(((JTextField) item).getText());
			} else if (item instanceof JComboBox) {
				Preconditions.checkState(((JComboBox) item).getSelectedIndex() == 0, "((JComboBox)item) is empty");
			}
		}

		return true;
	}
}